﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Util.Extension;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    public class EmailSettingController : BaseController
    {
        private readonly IWebHostEnvironment _hostingEnv;
        private readonly mtContext _context;

        public EmailSettingController(mtContext context, IWebHostEnvironment hostingEnv) : base(context, hostingEnv)
        {
            _context = context;
            _hostingEnv = hostingEnv;
        }

        #region 编辑信息
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            EmailSetting email = await _context.EmailSettings.FindAsync(id);
            if (email == null)
            {
                return NotFound();
            }
            return View(email);
        }
        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Edit(EmailSetting item)
        {
            Result res = new Result();
            try
            {
                EmailSetting model = await _context.EmailSettings.FindAsync(item.Id);
                if (model != null)
                {
                    model.Supplier = item.Supplier;
                    model.EnableSsl = false;
                    model.Address = item.Address;
                    model.Port = item.Port;
                    model.Account = item.Account;
                    model.Password = item.Password;
                    model.Email = item.Email;
                    model.From = item.From;
                    model.State = 1;

                    res.Code = 0;
                    res.Data = await _context.SaveChangesAsync();
                }
                else
                {
                    res.Code = 1;
                    res.Msg = "submitFail";
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion
    }
}
